package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.AdvertisingEncyclopediaQueryDTO;
import com.jeff.tianti.cms.dto.AdvertisingEncyclopediaTypeQueryDTO;
import com.jeff.tianti.cms.entity.AdvertisingEncyclopedia;
import com.jeff.tianti.cms.entity.AdvertisingEncyclopediaType;
import com.jeff.tianti.cms.entity.ColumnInfo;
import com.jeff.tianti.cms.service.AdvertisingEncyclopediaService;
import com.jeff.tianti.cms.service.AdvertisingEncyclopediaTypeService;
import com.jeff.tianti.common.ColumnInfoCom;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.PageModel;
import com.jeff.tianti.util.Constants;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/advertising")//广告百科控制器
public class AdvertisingEncyclopediaController {
    @Autowired
    private AdvertisingEncyclopediaService advertisingService;
    @Autowired
    private AdvertisingEncyclopediaTypeService advertisingEncyclopediaTypeService;

    /**
     * @Ceator:liuYI
     * @Describe:广告百科内容列表展示
     */
    @RequestMapping("/encyclopedia/list")
    public String encyclopediaList(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");//当前页
        String pageSizeStr = request.getParameter("pageSize");//每页显示条数
        String cmsId = request.getParameter("cmsId");//cms_column_info主键id
        String type = request.getParameter("type");//cms_column_info主键id
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        AdvertisingEncyclopediaQueryDTO dto = new AdvertisingEncyclopediaQueryDTO();
        dto.setCmsId(cmsId);
        dto.setPageSize(pageSize);
        dto.setCurrentPage(currentPage);
        dto.setType(type);
        PageModel<AdvertisingEncyclopedia> page = advertisingService.queryAdvertisingEncyclopediaPage(dto);
        List<AdvertisingEncyclopediaType> advertisingEncyclopediaTypes = advertisingEncyclopediaTypeService.queryAdvertisingEncyclopediaTypeList(null);
        model.addAttribute("list", advertisingEncyclopediaTypes);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("dto", dto);
        model.addAttribute(Constants.MENU_NAME, Constants.ADVERTISING_ENCYCLOPEDIA);
        return "user/advertisingEncyclopedia/advertisingEncyclopedia_list";
    }

    /**
     * @Ceator:liuYI
     * @Describe:用户编辑
     */
    @RequestMapping("/encyclopedia/edit")
    public String findEncyclopediaById(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        AdvertisingEncyclopedia advertisingEncyclopedia = new AdvertisingEncyclopedia();
        if (StringUtils.isNotBlank(id)) {
            advertisingEncyclopedia = advertisingService.find(id);
        }
        List<AdvertisingEncyclopediaType> advertisingEncyclopediaTypes = advertisingEncyclopediaTypeService.queryAdvertisingEncyclopediaTypeList(null);
        model.addAttribute("list", advertisingEncyclopediaTypes);
        model.addAttribute("advertisingEncyclopedia", advertisingEncyclopedia);
        return "user/advertisingEncyclopedia/edit/advertisingEncyclopedia_edit";
    }

    /**
     * @Ceator:liuYI
     * @Describe:添加广告百科内容
     */
    @RequestMapping("/encyclopedia/save")
    @ResponseBody
    public AjaxResult doAdvertisingSave(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String cmsId = request.getParameter("cmsId");
            String title = request.getParameter("title");
            String imgUrl = request.getParameter("imgUrl");
            String imgFile = request.getParameter("imgFile");
            String content = request.getParameter("content");
            String type = request.getParameter("type");//0->广告百科专页     1->广告百科首页
//            402881c26c1d89f4016c1d8acf1f0021   0->问答类型     1->百科类型
            if (!"0".equals(type)) {
                cmsId = "402881c26c1d89f4016c1d8acf1f0021";
            }
            AdvertisingEncyclopedia adv = new AdvertisingEncyclopedia();
            adv.setTitle(title);
            adv.setContent(content);
            adv.setImgFile(imgFile);
            adv.setImgUrl(imgUrl);
            adv.setCmsId(cmsId);
            adv.setType(type);
            if (StringUtils.isNotBlank(id)) {
                adv.setId(id);
                advertisingService.update(adv);
            } else {
                adv.setDeleteFlag("0");
                adv.setCreateDate(new Date());
                adv.setUpdateDate(new Date());
                advertisingService.save(adv);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @RequestMapping("/encyclopedia/del")
    @ResponseBody
    public AjaxResult doAdvertisingDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            if (StringUtils.isNotBlank(id)) {
                AdvertisingEncyclopedia advertisingEncyclopedia = advertisingService.find(id);
                String deleteFlag = advertisingEncyclopedia.getDeleteFlag();
                deleteFlag = "0".equals(deleteFlag) ? "1" : "0";
                advertisingEncyclopedia.setDeleteFlag(deleteFlag);
                advertisingService.update(advertisingEncyclopedia);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/encyclopediaType/edit")
    public String advertisingEncyclopediaTypeEdit(HttpServletRequest request, Model model) {
        String id = request.getParameter("id");
        AdvertisingEncyclopediaType advertisingEncyclopediaType = null;
        if (StringUtils.isNotBlank(id)) {
            advertisingEncyclopediaType = advertisingEncyclopediaTypeService.find(id);
        }
        model.addAttribute(Constants.MENU_NAME, Constants.ADVERTISING_ENCYCLOPEDIA_TYPE);
        model.addAttribute("advertisingEncyclopediaType", advertisingEncyclopediaType);
        return "/user/advertisingEncyclopedia/edit/advertisingEncyclopediaType_edit";
    }

    /**
     * @Ceator:liuYI
     * @Describe:问答百科行业分类添加
     */
    @RequestMapping("/encyclopediaType/save")
    @ResponseBody
    public AjaxResult saveAdvertisingEncyclopediaType(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String name = request.getParameter("name");
            String url = request.getParameter("url");
            String file = request.getParameter("file");
            if (StringUtils.isNotBlank(id)) {
                AdvertisingEncyclopediaType advertisingEncyclopediaType = advertisingEncyclopediaTypeService.find(id);
                advertisingEncyclopediaType.setUrl(url);
                advertisingEncyclopediaType.setFile(file);
                advertisingEncyclopediaType.setName(name);
                advertisingEncyclopediaTypeService.update(advertisingEncyclopediaType);
            } else {
                AdvertisingEncyclopediaType advertisingEncyclopediaType = new AdvertisingEncyclopediaType();
                advertisingEncyclopediaType.setName(name);
                advertisingEncyclopediaType.setFile(file);
                advertisingEncyclopediaType.setUrl(url);
                advertisingEncyclopediaTypeService.save(advertisingEncyclopediaType);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/encyclopediaType/del")
    @ResponseBody
    public AjaxResult advertisingEncyclopediaTypeDel(HttpServletRequest request) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        try {
            String id = request.getParameter("id");
            String deleteFlag = request.getParameter("deleteFlag");
            if (StringUtils.isNotBlank(id)) {
                AdvertisingEncyclopediaType advertisingEncyclopediaType = advertisingEncyclopediaTypeService.find(id);
                advertisingEncyclopediaType.setDeleteFlag(deleteFlag);
                advertisingEncyclopediaTypeService.update(advertisingEncyclopediaType);
            }
            ajaxResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ajaxResult;
    }

    @RequestMapping("/encyclopediaType/list")
    public String advertisingEncyclopediaTypeList(HttpServletRequest request, Model model) {
        String currentPageStr = request.getParameter("currentPage");//当前页
        String pageSizeStr = request.getParameter("pageSize");//每页显示条数
        int currentPage = 1;
        int pageSize = 10;
        if (StringUtils.isNotBlank(currentPageStr)) {
            currentPage = Integer.parseInt(currentPageStr);
        }
        if (StringUtils.isNotBlank(pageSizeStr)) {
            pageSize = Integer.parseInt(pageSizeStr);
        }
        AdvertisingEncyclopediaTypeQueryDTO dto = new AdvertisingEncyclopediaTypeQueryDTO();
        dto.setCurrentPage(currentPage);
        dto.setPageSize(pageSize);
        PageModel<AdvertisingEncyclopediaType> page = advertisingEncyclopediaTypeService.queryAdvertisingEncyclopediaTypePage(dto);
        model.addAttribute("page", page);
        model.addAttribute("pageSize", pageSize);
        model.addAttribute("dto", dto);
        model.addAttribute(Constants.MENU_NAME, Constants.ADVERTISING_ENCYCLOPEDIA_TYPE);
        return "user/advertisingEncyclopedia/advertisingEncyclopediaType_list";
    }
}
